﻿using MediatR;
using THE_WIND_RISES.Common.Dtos;
using Production_Set.API.Application.Commands.Supplier;
using AutoMapper;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;

namespace Production_Set.API.Application.Commands.Procurement
{
    /// <summary>
    /// 采购/委外订单
    /// </summary>
    public class PurchasingsCreateHandler : IRequestHandler<PurchasingsCreateCommand, Result>
    {
        /// <summary>
        /// 采购/委外订单头信息
        /// </summary>
        private readonly IBaseRepository<Purchasings> purchasingsRepository;
        /// <summary>
        /// 订单行
        /// </summary>
        private readonly IBaseRepository<OrderLines> orderLinesRepository;
        /// <summary>
        /// 付款计划
        /// </summary>
        private readonly IBaseRepository<PaymentPlans> paymentplansRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<PurchasingsCreateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingsRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="orderLinesRepository"></param>
        /// <param name="paymentplansRepository"></param>
        /// <param name="logger"></param>
        public PurchasingsCreateHandler(IBaseRepository<Purchasings> purchasingsRepository, IMapper mapper, IBaseRepository<OrderLines> orderLinesRepository, IBaseRepository<PaymentPlans> paymentplansRepository,ILogger<PurchasingsCreateHandler> logger)
        {
            this.purchasingsRepository = purchasingsRepository;
            this.mapper = mapper;
            this.orderLinesRepository = orderLinesRepository;
            this.paymentplansRepository = paymentplansRepository;
            this.logger = logger;
        }
        public async Task<Result> Handle(PurchasingsCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };

            try
            {
                string PurchasingSql = $"select * from Purchasings where PurchasingNo='{request.PurchasingNo}'";
                var PurchasingList = purchasingsRepository.GetAll(PurchasingSql).FirstOrDefault();
                if (PurchasingList != null)
                {
                    result.Code = (int)CommonErrorCodes.Added;
                    result.Data = null;
                    result.Msg = "编号重复 请重新输入";
                    logger.LogError("添加失败 编号重复 请重新输入");

                   
                  
                    return result;
                }
                else
                {
                    var AddData = mapper.Map<Purchasings>(request);
                    PurchasingSql = "insert into Purchasings values(@PurchasinId,@PurchasingNo,@PurchasingName,@PurchasingTypeId,@ProjectId,@PurchasingDesc,@SuppliersId,@OrderAmount,@Currency,@AdvancePayment,@StartTime,@EndTime,@SignUpTime,@PurchasingStatus,@TaxType,@TaxRate,@Billing,@PaidFor,@BillMaking,@DepartmentId,@ApprovalStatus,@IsDelete,@Contract,@Condition,@Salesperson,@LatestDate,@tests,@Handle,@HandleContext,@HandleTime,@Pid)";
                    var AddNum = purchasingsRepository.Add(PurchasingSql, new
                    {
                        PurchasinId = AddData.PurchasinId,
                        PurchasingNo = AddData.PurchasingNo,
                        PurchasingName = AddData.PurchasingName,
                        PurchasingTypeId = request.PurchasingTypeId,
                        ProjectId = request.ProjectId,
                        PurchasingDesc = AddData.PurchasingDesc,
                        SuppliersId = request.SuppliersId,
                        OrderAmount = AddData.OrderAmount,
                        Currency = AddData.Currency,
                        AdvancePayment = AddData.AdvancePayment,
                        StartTime = AddData.StartTime,
                        EndTime = AddData.EndTime,
                        SignUpTime = AddData.SignUpTime,
                        PurchasingStatus = AddData.PurchasingStatus,
                        TaxType = AddData.TaxType,
                        TaxRate = AddData.TaxRate,
                        Billing = AddData.Billing,
                        PaidFor = AddData.PaidFor,
                        BillMaking = AddData.BillMaking,
                        DepartmentId = request.DepartmentId,
                        ApprovalStatus = AddData.ApprovalStatus,
                        Contract = AddData.Contract,
                        Condition = AddData.Condition,
                        Salesperson = AddData.Salesperson,
                        LatestDate = AddData.LatestDate,
                        tests = AddData.tests,
                        Handle = request.Handle,
                        HandleContext = request.HandleContext,
                        HandleTime = DateTime.Now,
                        Pid = request.Pid,
                        IsDelete = AddData.IsDelete
                    });
                    if (AddNum > 0)
                    {

                        result.Code = (int)CommonErrorCodes.Success;
                        result.Data = null;
                        result.Msg = "采购/委外订单添加成功";
                        logger.LogInformation("采购/委外订单添加成功");
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError("采购/委外订单添加失败" + ex.Message);

                throw;
            }

            return result;
        }
    }
}
